const $params = function(obj) {
  const str = []
  for (const k in obj) {
    if (obj.hasOwnProperty(k)) {
      str.push(
        encodeURIComponent(k) +
        '=' +
        (typeof obj[k] === 'object' ? JSON.stringify(obj[k]) : obj[k])
      )
    }
  }
  return str.join('&')
}

const jsonP = function(url, data) {
  const global = window
  return new Promise((resolve, reject) => {
    const jsonPCallbackName = 'jsonP' + new Date().getTime()
    if (data && Object.keys(data).length > 0) {
      url = url.replace(/\?$/, '') + '?' + $params(data) + '&'
    } else {
      url = url.replace(/\?$/, '') + '?'
    }
    url += 'callback=' + jsonPCallbackName
    const script = document.createElement('script')
    script.src = url
    global[jsonPCallbackName] = function(data) {
      resolve(data)
      delete global[jsonPCallbackName]
      document.body.removeChild(script)
    }
    script.onerror = reject
    document.body.appendChild(script)
  })
}

export default jsonP

